<?php
/**
 * Subcategoryadmin_Data_Editsubcategory
 * edit subcategory
 * @author Adrian Stolarski
 */
class Subcategoryadmin_Data_Editsubcategory implements Subcategoryadmin_Data_Mainable, 
        Subcategoryadmin_Data_Preparable, Subcategoryadmin_Data_Templatable, 
        Subcategoryadmin_Data_Tokenable{
    
    /**
     *
     * @var type $database
     * database handler
     */
    public $database;
    /**
     *
     * @var type $database_query 
     * database query handler
     */
    public $database_query;
    /**
     *
     * @var type $random token
     * random token handler
     */
    public $random_token;
    /**
     *
     * @var type $all_categories_data;
     * get all categories data
     */
    public $all_categories_data;
    /**
     *
     * @var type $current_module_data
     * current module data handler
     */
    public $current_module_data;
    /**
     * constructor();
     */
    public function __construct() {
        $this->database = new Core_Driver_MysqlImproved;
        $this->random_token = new Core_Data_Tokenizer;
        $this->all_categories_data = new Core_Controller_Maincategorydata;
        $this->current_module_data = new Core_Controller_Currentmoduledata;
    }
    
    /**
     * create_token()
     * create random token
     * 
     * @return type Core_Data_Tokenizer
     */
    public function create_token() {
        return $this->random_token->create_token();
    }
    
    /**
     * get_template_name()
     * return current tempalte name
     * 
     * @return string $template
     */
    public function get_template_name() {
        $template = SERVER_ROOT."subcategoryadmin/views/editsubcategory.php";
        return $template;
    }
    
    /**
     * main()
     * create add sub category template data
     * @param array $variables
     * @return type $page
     */
    public function main(array $variables = null) {
        //var_dump($variables);
        //die();
        $page['template'] = $this->get_template_name();
        $page['csrf_token'] = $this->create_token();
        //var_dump($page);
        //die();
        $this->database->connect();
        foreach($variables as $key => $value){
            $variables[$key] = $this->database->escape($value);
        }
        $this->prepare_query($variables);
        //echo $this->database_query;
        //die();
        $this->database->prepare($this->database_query);
        $this->database->query();
        $page['sub_category_data'] = $this->database->fetch('array');
        $page['all_categories_data'] = $this->all_categories_data->main(null);
        $current_module['main_category_module_id'] = $page['sub_category_data']['sub_category_module_id'];
        $page['current_module_data'] = $this->current_module_data->main($current_module);
        //var_dump($page);
        //die();
        $page['current_main_category'] = $this->all_categories_data->main($page['sub_category_data']);
        //var_dump($page);
        //die();
        return $page;
    }
    
    /**
     * prapapre_query()
     * prepare database query
     * @param array $variables
     */
    public function prepare_query(array $variables = null) {
       $this->database_query = "SELECT * FROM page_sub_category WHERE sub_category_name='".$variables['sub_category_name']."';"; 
       //echo $this->database_query;
       //die();
    }

}
